<?php

namespace App\Http\Controllers\Admin;

use App\Models\Link;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Redirect;

class LinkController extends BaseController
{

    public function index()
    {
        if(!$this->user->can('viewContentLink'))
            abort(405);

        $links = Link::orderBy('id', 'DESC')->paginate(20);
        return view('admin.link.index')->with('links', $links);
    }

    public function create()
    {
        if(!$this->user->can('editContentLink'))
            abort(405);

        return view('admin.link.add')->with('blademode','add');
    }

    public function edit($id)
    {
        if(!$this->user->can('viewContentPosition'))
            abort(405);

        $link = Link::find($id);
        return view('admin.link.edit')->with('link', $link)->with('blademode','edit');

    }

    public function store(Request $request)
    {
        if(!$this->user->can('editContentLink'))
            abort(405);

        $data = $request->only('title','url','sort','is_disabled','id','blademode');

        if($data['blademode'] == 'edit' && is_numeric($data['id']) && $data['id'] > 0)
        {
            $link = Link::find($data['id']);
        }
        else
        {
            $link = new Link;
        }

        $link->title = $data['title'];
        $link->is_disabled = $data['is_disabled'];
        $link->sort = $data['sort'];
        $url=$data['url'];
        $preg = '/^(http|https):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&amp;)|&)+/';
        if(!preg_match($preg,$url)) {  //如果不能匹配
            $link->url = 'http://'.$data['url'];
        }else{
            $link->url =$data['url'];
        }
        if ($link->save()) {
            return redirect("admin/content/link");
        } else {
            return Redirect::back()->withInput()->withErrors('保存失败!');
        }
    }

    public function show($id)
    {
        if(!$this->user->can('viewContentLinkLink'))
            abort(405);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function updateStatus(Request $request)
    {
        if(!$this->user->can('editContentLink'))
            return $this->formatJson('error','抱歉，您没有相关权限');

        $id = $request->input('id');
        $is_disabled = $request->input('is_disabled');
        $link = Link::find($id);

        $link->is_disabled = $is_disabled = $is_disabled > 0 ? 0 : 1;
        if ($link->save())
            return  Response::json(array('status'=>'success','msg'=>'友情链接', 'data' => $is_disabled));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        if(!$this->user->can('deleteContentLink'))
            return $this->formatJson('error','抱歉，您没有相关权限');

        $link = Link::find($id);
        if ($link->delete())
            return  Response::json(array('status'=>'success','msg'=>'友情链接 [' . str_limit($link->title, 15, '...') . '] 删除成功'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }
}
